package com.apps.ftapp.activity;

import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.TimeLineAdapter;
import com.apps.ftapp.bean.LogisticsDetailModel;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class LogisticsDetailActivity extends BaseActivity {

    @BindView(R.id.order_shouhuoren)
    TextView orderShouhuoren;
    @BindView(R.id.order_phone)
    TextView orderPhone;
    @BindView(R.id.order_dizhi)
    TextView orderDizhi;
    @BindView(R.id.order_time)
    TextView orderTime;
    @BindView(R.id.order_no)
    TextView orderNo;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private List<LogisticsDetailModel.NwuliuBean> dataList = new ArrayList();
    private TimeLineAdapter mTimeLineAdapter;

    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_detail);
        ButterKnife.bind(this);

        setTitle("物流详情");

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String id = bundle.getString("id");

        L.i("main", "id:::" + id);


//        initView();


        loadData(id);


    }

    private void initView() {

        mTimeLineAdapter = new TimeLineAdapter(dataList);
        //创建默认的线性LayoutManager
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mTimeLineAdapter);
    }

    //
    private void loadData(String id) {


        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("id", id);
        L.i("main", ">>>>" + Constant.uid + ":::" + Constant.token);
        Async.post("my/wuliud", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {

                    L.i("main", new String(responseBody));

                    LogisticsDetailModel result = gson.fromJson(new String(responseBody), LogisticsDetailModel.class);

                    if (result.getAddress().size() > 0) {
                        orderShouhuoren.setText("收货人：" + result.getAddress().get(0).getUser_name());
                        orderPhone.setText(result.getAddress().get(0).getMobile());
                        orderDizhi.setText("收货人地址上：" + result.getAddress().get(0).getAddress());
                    }
                    orderTime.setText(result.getGongsiname());
                    orderNo.setText(result.getKuaididanhao());


                    dataList.addAll(result.getNwuliu());
                    initView();
//                    mTimeLineAdapter.addRes(result.getNwuliu());


                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });


    }
}
